<template>
  <div class="xtx-city" ref="target">
    <div class="select" @click="toggle" :class="{active: visible}">
      <span class="placeholder">请选择配送地址</span>
      <span class="value">{{fullLocation}}</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-show="visible">
      <div v-if="loading" class="loading"></div>
      <template v-else>
        <span @click="changeItem(item)" class="ellipsis" v-for="item in curList" :key="item.code">
          {{item.name}}
        </span>
      </template>
    </div>
  </div>
</template>

<script>
import { ref, reactive, computed } from 'vue'
import { onClickOutside } from '@vueuse/core'
import axios from 'axios'

// 获取省市区数据
const url = 'https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json'
const getCityData = () => {
  return axios({ url })
}
export default {
  name: 'XtxCity',
  props: { fullLocation: { type: String, default: '' } },
  setup (props, { emit }) {
    const visible = ref(false)

    const loading = ref(false)
    const close = () => {
      visible.value = false

      // 清空选择痕迹
      selectResult.provinceCode = '' // 省的code
      selectResult.provinceName = '' // 省的名字
      selectResult.cityCode = '' // 市的code
      selectResult.cityName = '' // 市的名字
      selectResult.countyCode = '' // 区的code
      selectResult.countyName = '' // 区的名字
      selectResult.fullLocation = ''// 组合
    }

    // 切换显示隐藏
    const toggle = () => {
      if (visible.value) {
        close()
      } else {
        open()
      }
    }

    const target = ref(null)
    onClickOutside(target, () => {
      // 关闭弹层
      close()
    })

    const cityData = ref([])

    const open = () => {
      visible.value = true
      //  检查是否在window中有数据
      if (window.cityData) {
        cityData.value = window.cityData
      } else {
        loading.value = true // 正在加载
        // ajax加载数据
        getCityData().then(res => {
          cityData.value = res.data
          // 向window这个超级对象中存入属性
          window.cityData = res.data

          loading.value = false // 加载完成
        })
      }
    }

    const selectResult = reactive({
      provinceCode: '', // 省的code
      provinceName: '', // 省的名字
      cityCode: '', // 市的code
      cityName: '', // 市的名字
      countyCode: '', // 区的code
      countyName: '', // 区的名字
      fullLocation: ''// 组合
    })

    // 当前显示的地区的内容，由用户的选择的变化来定的
    // 这里用计算属性
    const curList = computed(() => {
      let province = {}
      let result = cityData.value

      if (selectResult.provinceCode) {
        // 当前用户选择了省，返回对应的市级信息
        province = cityData.value.find(it => it.code === selectResult.provinceCode)
        result = province.areaList
      }
      if (selectResult.cityCode) {
        // 当前用户选择了市，返回对应的区级信息
        const city = province.areaList.find(it => it.code === selectResult.cityCode)
        result = city.areaList
      }

      return result
    })

    const changeItem = (item) => {
      console.log('当前点击的元素是', item)
      if (item.level === 0) {
        // 点击是省-保存用户的选择
        selectResult.provinceCode = item.code
        selectResult.provinceName = item.name
      }
      if (item.level === 1) {
        // 点击是市-保存用户的选择
        selectResult.cityCode = item.code
        selectResult.cityName = item.name
      }
      if (item.level === 2) {
        // 点击是区-保存用户的选择
        selectResult.countyCode = item.code
        selectResult.countyName = item.name
        // 通知父组件,当前用户的选择
        selectResult.fullLocation = selectResult.provinceName + selectResult.cityName + selectResult.countyName
        emit('address-change', selectResult)
        // 关闭弹层了
        close()
      }
    }

    return { visible, toggle, target, curList, changeItem, selectResult, loading }
  }
}
</script>
<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
  .loading {
    height: 290px;
    width: 100%;
    background: url("~@/assets/images/loading.gif") no-repeat center;
  }
}
</style>
